const crypto = require('crypto');

//加密公共密钥 32位
let key = '9ce62c1836d128cfc875c9026db7564c';
//向量
let iv = "";
class Aes_util {

    static encode(data){
        iv = iv || "";
        let clearEncoding = 'utf8';
        let cipherEncoding = 'base64';
        let cipherChunks = [];
        let cipher = crypto.createCipheriv('aes-256-ecb', key, iv);
        cipher.setAutoPadding(true);
        cipherChunks.push(cipher.update(data, clearEncoding, cipherEncoding));
        cipherChunks.push(cipher.final(cipherEncoding));
        return cipherChunks.join('');
    }

    static decode(data){
        if (!data) {
            return "";
        }
        try {
            iv = iv || "";
            let clearEncoding = 'utf8';
            let cipherEncoding = 'base64';
            let cipherChunks = [];
            let decipher = crypto.createDecipheriv('aes-256-ecb', key, iv);
            decipher.setAutoPadding(true);
            cipherChunks.push(decipher.update(data, cipherEncoding, clearEncoding));
            cipherChunks.push(decipher.final(clearEncoding));
            return cipherChunks.join('');
        } catch (e) {
            console.log(`解密失败 :${data}`);
            return "{\"status\":-200}";
        }
    }

}
// module.exports = Aes_util;
export default Aes_util
